package web.suzy.oj.service.file.impl;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import web.suzy.oj.common.exception.StatusFailException;
import web.suzy.oj.common.exception.StatusForbiddenException;
import web.suzy.oj.common.exception.StatusSystemErrorException;
import web.suzy.oj.common.result.ResultBean;
import web.suzy.oj.common.result.ResultStatus;
import web.suzy.oj.manager.file.TestCaseManager;
import web.suzy.oj.service.file.TestCaseService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * YangSuzy 软件工作室
 * 类名: TestCaseServiceImpl
 * 描述: ---- 待定 -----
 * 功能: ---- 待定 -----
 *
 * @author YangSuzy
 * Date: 2022/12/4 16:21
 */
@Service
public class TestCaseServiceImpl implements TestCaseService {
    @Resource
    private TestCaseManager testCaseManager;

    /**
     * 方法名: uploadTestcaseZip
     * 描述: 上传测试用例zip压缩文件
     *
     * @param file zip压缩文件
     * @param mode 评测模式
     * @return web.suzy.oj.common.result.ResultBean<java.util.Map < java.lang.Object, java.lang.Object>>
     * @date 2022/12/4 16:22
     * @auther YangSuzy
     **/
    @Override
    public ResultBean<Map<Object, Object>> uploadTestcaseZip(MultipartFile file, String mode) {
        try {
            return ResultBean.successResponse(testCaseManager.uploadTestcaseZip(file, mode));
        } catch (StatusFailException e) {
            return ResultBean.errorResponse(e.getMessage());
        } catch (StatusSystemErrorException e) {
            return ResultBean.errorResponse(e.getMessage(), ResultStatus.SYSTEM_ERROR);
        } catch (StatusForbiddenException e) {
            return ResultBean.errorResponse(e.getMessage(), ResultStatus.FORBIDDEN);
        }
    }

    /**
     * 方法名: downloadTestcase
     * 描述: 根据题号下载测试用例
     *
     * @param pid      题目ID
     * @param response 响应
     * @return void
     * @date 2022/12/4 16:34
     * @auther YangSuzy
     **/
    @Override
    public void downloadTestcase(Long pid, HttpServletResponse response) throws StatusFailException, StatusForbiddenException {
        testCaseManager.downloadTestcase(pid, response);
    }
}
